# 代理余量监控
import re
import json
import base64
import requests
from crawler.monitor.dingding_group_robot import DingDingGroupRobot

dding = DingDingGroupRobot()

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
}


def login():
    session = requests.session()
    session.get('https://www.qg.net/login/', headers=headers)
    data = {
        'username': 'jay169',
        'mobile': '13323868251',
        'password': 'wxydv8888'
    }
    session.post('https://www.qg.net/subAccount/login', headers=headers, data=data)
    cookies = session.cookies.get_dict()
    return cookies


cookies = login()


def result(params):
    c = json.loads(base64.b64decode(params).decode('utf-8'))
    return c


response = requests.get('https://www.qg.net/user/proxyipResource', headers=headers, cookies=cookies)
if 'atob(' in response.text:
    a = re.compile('var tableData = JSON.parse\(atob\("(.*?)"\)\)', re.S).findall(response.text)
    if a:
        res = result(a[0])
        available_num = res[0]['resource_available']
        if available_num < 10:
            dding.send_data('国外代理可用资源小于10G，请及时补充 @18217772447', types=True)
        else:
            dding.send_data(f'国外代理剩余可用资源为 {available_num} G')
